<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/js/layui-v2.4.5/layui/css/layui.css}" media="all" />
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all" />
</head>
<body  style="-webkit-backface-visibility: hidden;">

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>添加角色</legend>
</fieldset>

<form class="layui-form"  lay-filter="example">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" lay-verify="required" autocomplete="off" placeholder="请输入角色名"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <input type="text" name="roleDesc" lay-verify="required" autocomplete="off"  placeholder="请输入角色描述"  class="layui-input" value="">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark"  autocomplete="off"  placeholder="备注"  class="layui-input" value="">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">角色所属权限</label>
        <div class="layui-input-block" id="privs" name ="privs" lay-filter="test1">

        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block" style="text-align: center">
            <button type="button" class="layui-btn layui-btn-normal" id ="back">返回</button>
            <button   class="layui-btn" lay-submit lay-filter="save">保存</button>
        </div>
    </div>
</form>
<script th:src="@{/js/jquery-2.1.1.min.js}"></script>
<script th:src="@{/js/layui-v2.4.5/layui/layui.js}"></script>
<script th:src="@{/js/utils.js}"></script>
<script th:inline="javascript">

</script>
<style>
    .layui-form-label{    width: 100px!important;}
    .layui-input-block{    margin-left: 135px!important;}
</style>
<script>
    layui.config({
        base: path+'/js/public/selectPlus/'
    }).extend({
        selectPlus: 'selectPlus'
    });
    layui.use(['form','laydate','selectPlus'], function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : parent.layer;
            selectPlus = layui.selectPlus,
            $ = layui.jquery, laydate = layui.laydate;


        $("#back").click(function () {
            var index=layer.getFrameIndex(window.name); //获取当前窗口的name
            layer.close(index);
        })

        /*
         加载所有权限列表，供选择
         */
        var test;
        $.ajax({
            type: "POST",
            url:  path+'/priv/getAllPriv',
            dataType:"json",
            data:{},
            success: function (data) {
                test = selectPlus.render({
                    el: '#privs',
                    data:data,
                    label:["privName"],
                    valueName: "privName", //获取哪一个值填充
                    values: [],//默认选中的值
                    valueSeparator: "，"
                });

            },
            error: function (msg) {
            }
        });




        /**
         *
         *监听提交
         * */
        form.on('submit(save)', function(data){
            var data = data.field;
            var obj = test.getChecked();
            var privs = obj.data;
            console.log(privs);
            if(privs.length==0){
                layer.msg("请选择角色",{icon: 5})
                return false;
            }
            for(var i=0;i<privs.length;i++){
                data['privIds['+i+']']=privs[i].id
            }

            var load = layer.load();
            $.ajax({
                type: "POST",
                url:  path+'/role/saveRole',
                dataType:"json",
                data:data,
                success: function (data) {
                    layer.close(load);
                    if(data.code=="1"){
                        layer.msg(data.msg,{icon: 6})
                        window.parent.hasSave=true;
                        var index=layer.getFrameIndex(window.name); //获取当前窗口的name
                        layer.close(index);
                    }else{
                        layer.msg(data.msg,{icon: 5})
                    }
                },
                error: function (msg) {
                    layer.close(load);
                }
            });
            return false;
        });
    });

</script>

</body>
</html>